<template>
  <a-modal
    :open="open"
    :width="640"
    title="单项打折"
    centered
    :closable="false"
    ok-text="确认打折"
    :ok-button-props="{ disabled: !isCompleted }"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex justify-between ml-5">
      <div class="w-30 mr-2">
        {{ formState.payItemName }}<span class="text-gray-400 ml-3">{{ formState.num }}{{ formState.unit }}</span>
      </div>
      <div class="text-red-500">￥{{ formatAmount(formState.hjje) }}</div>
    </div>
    <a-divider class="!my-2.5" />
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="折扣率%" class="w-1/2" v-bind="validateInfos.zkl">
        <a-input v-model:value="formState.zkl" v-input:float.2 placeholder="请输入" @blur="onBlurDiscount" />
      </a-form-item>
      <a-form-item label="结算金额" class="w-1/2" v-bind="validateInfos.jsje">
        <a-input v-model:value="formState.jsje" v-input:float.2 placeholder="请输入" @blur="onBlurPrice" />
      </a-form-item>
      <a-divider class="m-0 mb-2.5" />
      <a-form-item label="批准人" class="w-1/2" v-bind="validateInfos.pzr">
        <a-input v-model:value="formState.pzr" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="密码" class="w-1/2" v-bind="validateInfos.pwd">
        <a-input-password v-model:value="formState.pwd" placeholder="请输入密码" autocomplete="new-password" :max-length="20" />
        <template #prefix>
          <LockOutlined class="text-primary text-base" />
        </template>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { LockOutlined } from '@ant-design/icons-vue'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  zkl: '',
  jsje: '',
  pzr: '',
  pwd: ''
})
const rulesState = ref({
  zkl: [{ required: true, message: '请输入折扣率' }],
  jsje: [{ required: true, message: '请输入结算金额' }]
  // pzr: [{ required: true, message: '请输入批准人' }],
  // pwd: [{ required: true, message: '请输入密码' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onBlurDiscount = () => {
  if (Number(formState.value.zkl) && Number(formState.value.zkl) > 100) {
    formState.value.zkl = 100
    return message.error('不能大于100')
  }
  if (+formState.value.zkl) {
    formState.value.jsje = formatAmount((Number(formState.value.zkl) / 100) * formatAmount(Number(formState.value.hjje)))
  }
}
const onBlurPrice = () => {
  if (+formState.value.jsje) {
    formState.value.zkl = formatAmount((Number(formState.value.jsje) / formatAmount(Number(formState.value.hjje))) * 100)
  }
}

const onOk = async () => {
  const res = await post('/operation/opex/getDiscountPermisson', { empId: formState.value.pzr })
  if (res.code !== 0) return message.error(res.msg)
  // if (Number(formState.value.zkl) < res.data.discountPercent) {
  //   return message.error('不能超过最大折扣率')
  // }
  if (Number(formState.value.zkl) < res.data.discountPercent) {
    return message.error(`当前您可打的最高折扣为<${res.data.discountPercent}%>`)
  }
  if (Number(formState.value.hjje) - Number(formState.value.jsje) > res.data.maxDiscountAmount) {
    return message.error(`当前您的最高减免金额为<${formatAmount(res.data.maxDiscountAmount)}>元`)
  }
  const params = {
    spids: [props.rowData.id],
    ...formState.value
  }
  emit('success', params)
}
const onCancel = () => {
  emit('update:open', false)
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
      formState.value.hjje = Number(formState.value.price) * Number(formState.value.num)
      formState.value.jsje = formatAmount(formState.value.hjje)
      formState.value.zkl = formatAmount((Number(formState.value.jsje) / formatAmount(Number(formState.value.hjje))) * 100)
      formState.value.jsje = formatAmount((Number(formState.value.zkl) / 100) * Number(formState.value.hjje))
    } else {
      resetFields()
    }
  }
)
</script>
<style lang="less" scoped></style>
